#!/usr/bin/env python
# encoding: utf-8
'''
@author: zhaowenpeng
@contact: winston@peipeiyun.com
@software: garner
@file: page.py
@time: 2020/7/20 10:39 上午
@desc:
'''

import urllib
import ujson
PAGE_SIZE = 50

class Paginator():
    """系统查询分页工具"""
    def __init__(self, url_func, page=1, total=0, page_size=10):

        self.url_func = url_func
        self.page = 1 if int(page) < 1 else int(page)   #当前页
        self.total = int(total)   #总条数
        self.page_size = int(page_size)
        #总页数
        if self.total==0:
            self.page_num=1
        else:
            self.page_num = (self.total%self.page_size==0) and int(self.total/self.page_size) or int(self.total/self.page_size)+1
        if self.page>self.page_num:
            self.page = self.page_num
        self.page_bars = {}
        #开始数
        self.page_start=(self.page-1)*page_size+1
        #尾数
        self.page_end=self.page*page_size
        self.page_data={'page':self.page,
                        'page_size':self.page_size,
                        'page_num':self.page_num,
                        'total':self.total
                        }
        for _page in range(1, self.page_num + 1):
            _index = int(_page / 10)
            if not _index in self.page_bars:
                self.page_bars[_index] = {_page}
            else:
                self.page_bars[_index].add(_page)

    def to_dict(self):
        return {key:getattr(self,key) for key in ['page','total','page_size','page_num','page_start','page_end']}

